<script type="text/javascript">
  window.onload = function(){
    new JsDatePick({ useMode:2, target:"dfrom" });
    new JsDatePick({ useMode:2, target:"dto" });
  };

  function checkallfields() {
    for(e=0; e<document.myform.elements.length;e++) {
      if((document.myform.elements[e].type== "checkbox")){
        document.myform.elements[e].checked=true;
      }
    }
  };
  
  function lastnight() {
    var myDate = new Date();
    document.filterForm.dmin.value=10;
    document.filterForm.limit.value=500;
    document.filterForm.dfrom.value=myDate.getFullYear()+'-'+
                                    (myDate.getMonth()+1)+'-'+
                                    (myDate.getDate()-1)+' 19:00';
    document.filterForm.dto.value=myDate.getFullYear()+'-'+
                                  (myDate.getMonth()+1)+'-'+
                                  myDate.getDate()+' 09:00';
  };
</script>

<h1><%=t 'alerts.filter_label' %></h1>
<% form_tag '/alerts/',{:method=>:get,:name=>'filterForm' } do %>
  <table>
  <tr>
    <th><%=t 'alerts.filter_duration' %></th>
    <th><%=t 'alerts.filter_date' %></th>
    <th><%=t 'alerts.filter_include' %></th>
    <th><%=t 'alerts.filter_exclude' %></th>
    <th><%=t 'alerts.filter_limit' %></th>
  </tr>
  <tr>
    <td><%=t 'alerts.filter_minimal' %>: <%= text_field_tag :dmin, @alertFilter.dmin,:size => 3 %></td>
    <td><%=t 'alerts.filter_maximal' %>: <%= text_field_tag :dfrom, @alertFilter.dfrom,:size => 16 %></td>
    <td><%=t 'alerts.filter_hostname' %>: <%= text_field_tag :ihost, @alertFilter.ihost %></td>
    <td><%=t 'alerts.filter_hostname' %>: <%= text_field_tag :ehost, @alertFilter.ehost %></td>
    <td><%= text_field_tag :limit, @alertFilter.limit,{:size => 3,:class => @limitStyle }%></td>
  </tr>
  <tr>
    <td><%=t 'alerts.filter_maximal' %>: <%= text_field_tag :dmax, @alertFilter.dmax,:size => 3 %></td>
    <td><%=t 'alerts.filter_date_to' %>: <%= text_field_tag :dto, @alertFilter.dto,:size => 16 %></td>
    <td><%=t 'alerts.filter_service' %>: <%= text_field_tag :idesc, @alertFilter.idesc %></td>
    <td><%=t 'alerts.filter_service' %>: <%= text_field_tag :edesc, @alertFilter.edesc %></td>
  </tr>
  </table>
  <%= submit_tag t('alerts.filter_action') %> | 
  <%= button_to_function t('alerts.filter_helper_night') ,"lastnight()" %>
<% end %>

<h1><%=t 'alerts.listing_alerts' %> (<%=@limitLabel%>/<%=@alerts.size%>)</h1>

<% form_tag '/alerts/link_to_report',{:method=>:post,:name => :myform} do %>
    <% if logged? %>
    <%=t 'alerts.report' %>: 
    <%= select_tag "report_id", "<option value=\"0\"></option>" +
      options_from_collection_for_select(@reportList, :id, :label) %>
    <%= submit_tag %> | 
    <%= button_to_function t('alerts.check_all_boxes') ,"checkallfields()" %>
    <% end %>
<table>
  <tr>
    <th><%=t 'alerts.hostname' %></th>
    <th><%=t 'alerts.service' %></th>
    <th><%=t 'alerts.date_start' %></th>
    <th><%=t 'alerts.date_end' %></th>
    <th><%=t 'alerts.status' %></th>
    <th><%=t 'alerts.output' %></th>
    <th><%=t 'alerts.duration' %></th>
    <th><%=t 'alerts.report' %></th>
    <% if logged? %><th></th><% end %>
<%
=begin %>
    <th></th>
    <th></th>
    <th></th>
<%
=end %>
  </tr>

<% #prepare the table background
  bgColorPreviousDay = 0
  bgColorList = [ 'bg0', 'bg1' ]
  bgColorCurrent = 1
%>

<% @alerts.each do |alert| 
   
  #get the report class element
  reportClasses=""
  alert.report.reporttags.each { |reporttag|
    reportClasses=reporttag.css_classes+' '+reportClasses unless reporttag.css_classes.nil?
  } unless alert.report.nil?

  #set bg color
  if bgColorPreviousDay != alert.date_start.strftime("%d")
    bgColorCurrent == 0 ? bgColorCurrent = 1 : bgColorCurrent = 0
    bgColorPreviousDay = alert.date_start.strftime("%d")
  end
  %>

  <tr class="bg<%= bgColorCurrent %>">
    <td><%=h alert.service.hostname%></td>
    <td><%=h alert.service.description%></td>
    <td><%=h alert.date_start.strftime("%d/%m %H:%M") %></td>
    <td><%=h alert.date_end.strftime("%d/%m %H:%M") %></td>
    <td>
      <img src="/images/check-status-<%=h alert.check_status %>.png" alt="<%=h alert.check_status %>" />
    </td>
    <td><%=h alert.service_output %></td>
    <td class="<%=getDurationCssClass alert.duration %>">
      <%=h distance_of_time_in_words(0,alert.duration) %>
    </td>
    <td>
    <% if ! alert.report.nil? %>
      <a class="<%= reportClasses %>" 
          href="<%= url_for(:controller=>"reports", :action=>"show",:id=>alert.report_id) %>">
        <%=h alert.report.label %>
      </a>
    <% end %>
    </td>
    <% if logged? %><td><%= check_box_tag 'alert;'+alert.id.to_s, alert.id %></td><% end %>

<%
=begin %>
    <td><%= button_to "test",:action => :link_to_report, :id =>alert%> </td>
    <td><%= link_to 'Show', alert %></td>
    <td><%= link_to 'Edit', edit_alert_path(alert) %></td>
    <td><%= link_to 'Destroy', alert, :confirm => 'Are you sure?', :method => :delete %></td>
<%
=end %>

  </tr>
<% end %>
</table>

<% end %>
<br />

<%= link_to t('alerts.new_alert'), new_alert_path if logged? %>
